/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package czStvDpds.graphics3D.gui;

import czStvDpds.graphics3D.GraphicsIndex;
import czStvDpds.graphics3D.geometry.Mesh;
import java.awt.Color;
import java.awt.Font;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import org.lwjgl.BufferUtils;
import org.newdawn.slick.UnicodeFont;
import org.newdawn.slick.font.effects.ColorEffect;

/**
 * Slouží jako sklad statickych metod, ktere modeluji GUI. Diky jejimu pouziti
 * jsou tridy jednotlivych komponent uhlednejsi a prehlednejsi.
 *
 * @author Ondra
 */
public class Modeler {

    //obecne
    public static UnicodeFont getDefaultFont() {
        Font f = new Font("Sansserif", Font.TRUETYPE_FONT, 30);
        UnicodeFont uf = new UnicodeFont(f);
        uf.getEffects().add(new ColorEffect(Color.BLACK));
        uf.addAsciiGlyphs();
        uf.addNeheGlyphs();
        GraphicsIndex.getGLSynchronizer().generateGlyphs(uf);
        return uf;
    }

    public static int ratioToPixels(float ratio) {
        return (int) (ratio * SuperContainer.getActiveScene().getWidth());
    }

    public static float pixelsToRatio(int pixels) {
        return (float) pixels / (float) SuperContainer.getActiveScene().getWidth();
    }

    //--------------------------------------------------------------------------
    //LButton
    public static Mesh generateButtonMesh(float width, float height) {
        IntBuffer indices = BufferUtils.createIntBuffer(6 * 5);
        indices.put(new int[]{0, 1, 2, 0, 2, 3, 4, 5, 6, 4, 6, 7, 8, 9, 10, 8, 10, 11, 12, 13, 14, 12, 14, 15, 16, 17, 18, 16, 18, 19});
        indices.rewind();
        Mesh mesh = new Mesh(resizeButtonMesh(width, height), indices, 1);
        return mesh;
    }

    public static FloatBuffer resizeButtonMesh(float width, float height) {
        float fw = width != 0 ? 0.01F : 0;
        float fh = height != 0 ? 0.01F : 0;
        FloatBuffer data = BufferUtils.createFloatBuffer(36 * 5);
        data.put(new float[]{fw, fh, 0, 0, 0, 1, 0, 0, 0,
                    0, height - fw, 0, 0, 0, 1, 0, 1, 0,
                    width - fw, height - fh, 0, 0, 0, 1, 0.5F, 1, 0,
                    width - fw, 0, 0, 0, 0, 1, 0.5F, 0, 0,
                    0, 0, 0, 0, 0, 1, 0.5F, 0, 0,
                    0, height, 0, 0, 0, 1, 0.5F, 1, 0,
                    fw, height, 0, 0, 0, 1, 1, 1, 0,
                    fw, 0, 0, 0, 0, 1, 1, 0, 0,
                    width - fw, 0, 0, 0, 0, 1, 0.5F, 0, 0,
                    width - fw, height, 0, 0, 0, 1, 0.5F, 1, 0,
                    width, height, 0, 0, 0, 1, 1, 1, 0,
                    width, 0, 0, 0, 0, 1, 1, 0, 0,
                    0, 0, 0, 0, 0, 1, 0.5F, 0, 0,
                    0, fh, 0, 0, 0, 1, 0.5F, 1, 0,
                    width, fh, 0, 0, 0, 1, 1, 1, 0,
                    width, 0, 0, 0, 0, 1, 1, 0, 0,
                    0, height - fh, 0, 0, 0, 1, 0.5F, 0, 0,
                    0, height, 0, 0, 0, 1, 0.5F, 1, 0,
                    width, height, 0, 0, 0, 1, 1, 1, 0,
                    width, height - fh, 0, 0, 0, 1, 1, 0, 0});
        data.rewind();
        return data;
    }

    //--------------------------------------------------------------------------
    //LLabel
    public static FloatBuffer resizeLabelMesh(float width, float height) {
        FloatBuffer data = BufferUtils.createFloatBuffer(36);
        data.put(new float[]{0, 0, 0, 0, 0, 1, 0, 0, 0,
                    0, height, 0, 0, 0, 1, 0, 1, 0,
                    width, height, 0, 0, 0, 1, 1, 1, 0,
                    width, 0, 0, 0, 0, 1, 1, 0, 0});
        data.rewind();
        return data;
    }

    public static Mesh generateLabelMesh(float width, float height) {
//        FloatBuffer data = BufferUtils.createFloatBuffer(36);
//        data.put(new float[]{0, 0, 0, 0, 0, 1, 0, 0, 0, 0, height, 0, 0, 0, 1, 0, 1, 0, width, height, 0, 0, 1, 0, 1, 1, 0, width, 0, 0, 0, 1, 0, 1, 0, 0});
//        data.rewind();
        IntBuffer indices = BufferUtils.createIntBuffer(6);
        indices.put(new int[]{0, 1, 2, 0, 2, 3});
        indices.rewind();
        Mesh mesh = new Mesh(resizeLabelMesh(width, height), indices, 1);
        return mesh;
    }

    //--------------------------------------------------------------------------
    //LTextfield
    public static Mesh generateTextFieldMesh(float width, float height) {
//        float fw = width != 0 ? 0.01F : 0;
//        float fh = height != 0 ? 0.01F : 0;
//        FloatBuffer data = BufferUtils.createFloatBuffer(36 * 5);
//        data.put(new float[]{fw, fh, 0, 0, 0, 1, 0, 0, 0, 0, height - fw, 0, 0, 0, 1, 0, 1, 0, width - fw, height - fh, 0, 0, 1, 0, 0.5F, 1, 0, width - fw, 0, 0, 0, 1, 0, 0.5F, 0, 0, 0, 0, 0, 0, 0, 1, 0.5F, 0, 0, 0, height, 0, 0, 0, 1, 0.5F, 1, 0, fw, height, 0, 0, 1, 0, 1, 1, 0, fw, 0, 0, 0, 1, 0, 1, 0, 0, width - fw, 0, 0, 0, 0, 1, 0.5F, 0, 0, width - fw, height, 0, 0, 0, 1, 0.5F, 1, 0, width, height, 0, 0, 1, 0, 1, 1, 0, width, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0.5F, 0, 0, 0, fh, 0, 0, 0, 1, 0.5F, 1, 0, width, fh, 0, 0, 1, 0, 1, 1, 0, width, 0, 0, 0, 1, 0, 1, 0, 0, 0, height - fh, 0, 0, 0, 1, 0.5F, 0, 0, 0, height, 0, 0, 0, 1, 0.5F, 1, 0, width, height, 0, 0, 1, 0, 1, 1, 0, width, height - fh, 0, 0, 1, 0, 1, 0, 0});
//        data.rewind();
        IntBuffer indices = BufferUtils.createIntBuffer(6 * 5);
        indices.put(new int[]{0, 1, 2, 0, 2, 3, 4, 5, 6, 4, 6, 7, 8, 9, 10, 8, 10, 11, 12, 13, 14, 12, 14, 15, 16, 17, 18, 16, 18, 19});
        indices.rewind();
        Mesh mesh = new Mesh(resizeTextFieldMesh(width, height), indices, 1);
        return mesh;
    }

    public static FloatBuffer resizeTextFieldMesh(float width, float height) {
        float fw = width != 0 ? 0.01F : 0;
        float fh = height != 0 ? 0.01F : 0;
        FloatBuffer data = BufferUtils.createFloatBuffer(36 * 5);
        data.put(new float[]{fw, fh, 0, 0, 0, 1, 0, 0, 0,
                    0, height - fw, 0, 0, 0, 1, 0, 1, 0,
                    width - fw, height - fh, 0, 0, 0, 1, 0.5F, 1, 0,
                    width - fw, 0, 0, 0, 0, 1, 0.5F, 0, 0,
                    0, 0, 0, 0, 0, 1, 0.5F, 0, 0,
                    0, height, 0, 0, 0, 1, 0.5F, 1, 0,
                    fw, height, 0, 0, 0, 1, 1, 1, 0,
                    fw, 0, 0, 0, 0, 1, 1, 0, 0,
                    width - fw, 0, 0, 0, 0, 1, 0.5F, 0, 0,
                    width - fw, height, 0, 0, 0, 1, 0.5F, 1, 0,
                    width, height, 0, 0, 0, 1, 1, 1, 0,
                    width, 0, 0, 0, 0, 1, 1, 0, 0,
                    0, 0, 0, 0, 0, 1, 0.5F, 0, 0,
                    0, fh, 0, 0, 0, 1, 0.5F, 1, 0,
                    width, fh, 0, 0, 0, 1, 1, 1, 0,
                    width, 0, 0, 0, 0, 1, 1, 0, 0,
                    0, height - fh, 0, 0, 0, 1, 0.5F, 0, 0,
                    0, height, 0, 0, 0, 1, 0.5F, 1, 0,
                    width, height, 0, 0, 0, 1, 1, 1, 0,
                    width, height - fh, 0, 0, 0, 1, 1, 0, 0});
        data.rewind();
        return data;
    }

    //--------------------------------------------------------------------------
    //LCheckbox
    public static Mesh generateCheckboxMesh(float width, float height) {
        IntBuffer indices = BufferUtils.createIntBuffer(6 * 7);
        indices.put(new int[]{0, 1, 2, 0, 2, 3, 4, 5, 6, 4, 6, 7, 8, 9, 10, 8, 10, 11, 12, 13, 14, 12, 14, 15, 16, 17, 18, 16, 18, 19, 20, 21, 22, 20, 22, 23, 24, 25, 26, 24, 26, 27});
        indices.rewind();
        Mesh mesh = new Mesh(resizeCheckboxMesh(width, height), indices, 1);
        return mesh;
    }

    public static FloatBuffer resizeCheckboxMesh(float width, float height) {
        float fw = width != 0 ? 0.01F : 0;
        float fh = height != 0 ? 0.01F : 0;
        float a = height;
        float a2 = (a - (2 * fw)) * (2f / 3f);
        float fa = (a - a2) / 2;
        FloatBuffer data = BufferUtils.createFloatBuffer(36 * 7);
//        cast - text
        data.put(new float[]{a, 0, 0, 0, 0, 1, 0, 0, 0,
                    a, a, 0, 0, 0, 1, 0, 1, 0,
                    width, a, 0, 0, 0, 1, 0.25f, 1, 0,
                    width, 0, 0, 0, 0, 1, 0.25f, 0, 0,
                    //cast - krizek - vnitrek
                    0, 0, 0, 0, 0, 1, 0.75f, 0, 0,
                    0, a, 0, 0, 0, 1, 0.75f, 1, 0,
                    a, a, 0, 0, 0, 1, 1, 1, 0,
                    a, 0, 0, 0, 0, 1, 1, 0, 0,
                    //ram horni
                    0, 0, 0, 0, 0, 1, 0.25f, 0, 0,
                    0, fh, 0, 0, 0, 1, 0.25f, 1, 0,
                    a, fh, 0, 0, 0, 1, 0.5f, 1, 0,
                    a, 0, 0, 0, 0, 1, 0.5f, 0, 0,
                    //ram leva
                    0, 0, 0, 0, 0, 1, 0.25f, 0, 0,
                    0, a, 0, 0, 0, 1, 0.25f, 1, 0,
                    fw, a, 0, 0, 0, 1, 0.5f, 1, 0,
                    fw, 0, 0, 0, 0, 1, 0.5f, 0, 0,
                    //ram dolni
                    0, a - fh, 0, 0, 0, 1, 0.25f, 0, 0,
                    0, a, 0, 0, 0, 1, 0.25f, 1, 0,
                    a, a, 0, 0, 0, 1, 0.5f, 1, 0,
                    a, a - fh, 0, 0, 0, 1, 0.5f, 0, 0,
                    //ram prava
                    a - fw, 0, 0, 0, 0, 1, 0.25f, 0, 0,
                    a - fw, a, 0, 0, 0, 1, 0.25f, 1, 0,
                    a, a, 0, 0, 0, 1, 0.5f, 1, 0,
                    a, 0, 0, 0, 0, 1, 0.5f, 0, 0,
                    //check
                    fa, fa, 0, 0, 0, 1, 0.5f, 0, 0,
                    fa, fa + a2, 0, 0, 0, 1, 0.5f, 1, 0,
                    fa + a2, fa + a2, 0, 0, 0, 1, 0.75f, 1, 0,
                    fa + a2, fa, 0, 0, 0, 1, 0.75f, 0, 0});
        data.rewind();
        return data;
    }
}
